import {
  getDetailMenu,
  setBrowseNum
} from '../../api/index'
import {
  formatTime
} from '../../utils/util'
Page({
  data: {
    loading: true,
    menuInfo: {}
  },
  $data: {
    id: '',
    browseTimer: null,
    browseTime: 30 * 1000
  },

  onLoad: function (options) {
    const id = options.id
    this.$data.id = id
    getDetailMenu({
      id
    }).then(res => {
      res.data.update_time = formatTime(res.data.update_time)
      this.setData({
        loading: false,
        menuInfo: res.data
      })
    })
  },

  onShow() {
    const {
      id,
      browseTime
    } = this.$data
    this.$data.browseTimer = setTimeout(() => {
      setBrowseNum({
        id
      })
    }, browseTime)
  },

  onHide() {
    clearTimeout(this.$data.browseTimer)
  },

  onUnload() {
    clearTimeout(this.$data.browseTimer)
  },

  onPreview(e) {
    const currentUrl = e.currentTarget.dataset.src
    wx.previewImage({
      current: currentUrl,
      urls: [currentUrl]
    })
  },
  // 添加此事件就可以发送到朋友圈
  onShareAppMessage() {},

  // 分享朋友圈，前提条件设置了onShareAppMessage事件即可
  onShareTimeline() {
    return {
      title: '对味菜谱'
    }
  }
})